//
//  SCHotelHomeController.m
//  shangcheng
//
//  Created by Jie Jie on 2019/7/1.
//  Copyright © 2019 张艳江. All rights reserved.
//

#import "SCHotelHomeController.h"
#import "SCHotelHomeCell.h"
#import "SCHotelInfoController.h"

#define k_date_view_h                kScreenW * 44/375
#define k_date_label_w               70
#define k_date_timer_label_w         50
#define k_date_button_w              kScreenW - 70 - 50
#define k_date_button_h              30
#define k_table_header_view_space    5
#define k_table_header_view_h        k_date_view_h * 2 + k_table_header_view_space


@interface SCHotelHomeController ()<UITableViewDelegate , UITableViewDataSource , UISearchBarDelegate>

@property (nonatomic , strong) UIButton *back_button;

@property (nonatomic , strong) UIButton *address_button;
@property (nonatomic , strong) UILabel *address_label;
@property (nonatomic , strong) NSString *address_text;

@property (nonatomic , strong) UISearchBar *search_bar;
@property (nonatomic , strong) NSString *search_bar_placeholder;

@property (nonatomic , strong) UITableView *table_view;
@property (nonatomic , strong) UIView *header_view;

@property (nonatomic , strong) UIButton *date_button;
@property (nonatomic , strong) UILabel *date_label;
@property (nonatomic , strong) NSString *date_text;
@property (nonatomic , strong) UILabel *timer_label;
@property (nonatomic , strong) NSString *timer_text;
@property (nonatomic , strong) UIButton *sort_button;//排序
@property (nonatomic , strong) UIButton *screen_button;//筛选

@end

@implementation SCHotelHomeController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.search_bar_placeholder = [NSString string];
    self.search_bar_placeholder = @"请输入关键字";
    
    self.address_text = [NSString string];
    self.address_text = @"泉州";
    self.date_text = [NSString string];
    self.date_text = @"2019/08/08 —— 2019/08/09";
    self.timer_text = [NSString string];
    self.timer_text = @"一晚";

    [self loadHotelListData];
    [self createUI];
    
}

#pragma mark 私有方法 -- UI
-(void)createUI{
    
    // 隐藏默认导航栏
    self.navigationController.navigationBarHidden = NO;// 使右滑返回手势可用
    self.navigationController.navigationBar.hidden = YES;// 隐藏导航栏
    
    CGSize size = [self.address_label.text sizeWithAttributes:@{NSFontAttributeName:self.address_label.font}];
    
    CGFloat back_button_w = 30;
    CGFloat lr_space = 16;
    CGFloat address_img_w = 5;
    CGFloat address_lr_space = 8;
    CGFloat address_button_w = size.width + address_lr_space +address_img_w + lr_space;
    CGFloat search_bar_h = 29;
    CGFloat search_bar_y = (kNavBarHeight - search_bar_h)*0.5;
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, kStatusBarHeight, kScreenW, kNavBarHeight)];
    UIColor *color =  self.navigationController.navigationBar.barTintColor;
    [view setBackgroundColor:color];
    
    UIImageView *back_img = [[UIImageView alloc] init];
    back_img.image = [UIImage imageNamed:@"back"];
    [self.back_button addSubview:back_img];
    
    UIImageView *address_img = [[UIImageView alloc] init];
    address_img.image = [UIImage imageNamed:@"more_rig"];
    [self.address_button addSubview:address_img];
    [self.address_button addSubview:self.address_label];
    
    [view addSubview:self.back_button];
    [view addSubview:self.address_button];
    [view addSubview:self.search_bar];
    
    [self.view addSubview:view];
    [self.view addSubview:self.table_view];
   
    [self.back_button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.and.bottom.mas_equalTo(0);
        make.width.mas_equalTo(back_button_w);
    }];
    
    [back_img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(7);
        make.width.mas_equalTo(10);
        make.height.mas_equalTo(17);
        make.top.mas_equalTo((kNavBarHeight - 17) * 0.5);
    }];
    
    [self.address_button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.bottom.mas_equalTo(0);
        make.left.mas_equalTo(self.back_button.mas_right);
        make.width.mas_equalTo(address_button_w);
    }];
    
    [self.address_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.top.and.bottom.mas_equalTo(0);
        make.width.mas_equalTo(size.width);
    
    }];
    
    [address_img mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.mas_equalTo(self.address_label.mas_right).mas_offset(address_lr_space);
        make.height.mas_equalTo(8);
        make.top.mas_equalTo((kNavBarHeight - 8) * 0.5);
        make.width.mas_equalTo(address_img_w);
    }];
 
    [self.search_bar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.address_button.mas_right);
        make.top.mas_equalTo(search_bar_y);
        make.height.mas_equalTo(search_bar_h);
        make.right.mas_equalTo(-9);
    }];
   
    [self.table_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.and.right.mas_equalTo(0);
        make.top.mas_equalTo(view.mas_bottom);
        make.bottom.mas_equalTo(0);
        
    }];
   
}

-(UIView *)tabelHeaderViewInTopSection{
    
    UIView *header_view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, k_table_header_view_h)];
    header_view.backgroundColor = [UIColor clearColor];
    
    UIView *date_view = [[UIView alloc] init];
    date_view.backgroundColor = [UIColor whiteColor];
    [header_view addSubview:date_view];
    
    UIView *sort_view = [[UIView alloc] init];
    sort_view.backgroundColor = [UIColor whiteColor];
    [header_view addSubview:sort_view];
    
    UILabel *label = [[UILabel alloc] init];
    label.text = @"选择日期";
    label.font = [UIFont systemFontOfSize:13.0f];
    label.textAlignment = NSTextAlignmentCenter;
    [date_view addSubview:label];
    
    UIImageView *date_img = [[UIImageView alloc] init];
    date_img.image = [UIImage imageNamed:@"rl"];
    [self.date_button addSubview:self.date_label];
    [self.date_button addSubview:date_img];
    
    [date_view addSubview:self.date_button];
    [date_view addSubview:self.timer_label];
    
    UILabel *sort_label = [[UILabel alloc] init];
    sort_label.text = @"综合排序";
    sort_label.font = [UIFont systemFontOfSize:13.0f];
    UIImageView *sort_img = [[UIImageView alloc] init];
    sort_img.image = [UIImage imageNamed:@"more_bottom"];
    [self.sort_button addSubview:sort_label];
    [self.sort_button addSubview:sort_img];
    
    UILabel *screen_label = [[UILabel alloc] init];
    screen_label.text = @"筛选";
    screen_label.font = [UIFont systemFontOfSize:13.0f];
    UIImageView *screen_img = [[UIImageView alloc] init];
    screen_img.image = [UIImage imageNamed:@"more_bottom"];
    [self.screen_button addSubview:screen_label];
    [self.screen_button addSubview:screen_img];

    [sort_view addSubview:self.sort_button];
    [sort_view addSubview:self.screen_button];
    
    [date_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(1);
        make.left.and.right.mas_equalTo(0);
        make.height.mas_equalTo(k_date_view_h);
    }];
    
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.and.bottom.mas_equalTo(0);
        make.width.mas_equalTo(k_date_label_w);
    }];
    
    [self.date_button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(label.mas_right);
        make.top.mas_equalTo((k_date_view_h - k_date_button_h) * 0.5);
        make.width.mas_equalTo(k_date_button_w);
        make.height.mas_equalTo(k_date_button_h);

    }];
    
    [self.date_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(8);
        make.top.and.bottom.mas_equalTo(0);
        make.right.mas_equalTo(date_img.mas_left);
    }];

    [date_img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo((k_date_button_h - 13) * 0.5);
        make.width.mas_equalTo(13);
        make.height.mas_equalTo(13);
        make.right.mas_equalTo(-10);
    }];
    
    [self.timer_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.date_button.mas_right);
        make.right.and.top.and.bottom.mas_equalTo(0);
        make.width.mas_equalTo(k_date_timer_label_w);
    }];
    
    CGFloat lr_space = 2;
    CGFloat more_space = 5;
    CGFloat sort_img_w = 8;
    CGSize size = [sort_label.text sizeWithAttributes:@{NSFontAttributeName:sort_label.font}];
    CGFloat sort_button_w = size.width + sort_img_w + lr_space + more_space;
    
    CGSize size_screen = [screen_label.text sizeWithAttributes:@{NSFontAttributeName:screen_label.font}];
    CGFloat screen_button_w = size_screen.width + sort_img_w + lr_space + more_space;
    
    
    [sort_view mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.mas_equalTo(date_view.mas_bottom).mas_offset(k_table_header_view_space);
        make.left.and.right.mas_equalTo(0);
        make.height.mas_equalTo(k_date_view_h - 2);
    }];
    
    [self.sort_button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10);
        make.top.and.bottom.mas_equalTo(0);
        make.width.mas_equalTo(sort_button_w);
    }];
    
    [sort_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.width.mas_equalTo(size.width + more_space);
        make.height.mas_equalTo(20);
        make.top.mas_equalTo((k_date_view_h - 20) * 0.5);
        
    }];
    
    [sort_img mas_makeConstraints:^(MASConstraintMaker *make) {
    make.left.mas_equalTo(sort_label.mas_right).mas_offset(lr_space);
        make.height.mas_equalTo(5);
        make.top.mas_equalTo((k_date_view_h - 5) * 0.5);
        make.width.mas_equalTo(sort_img_w);
    }];
    
    [self.screen_button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.bottom.mas_equalTo(0);
        make.right.mas_equalTo(-10);
        make.width.mas_equalTo(screen_button_w);
    }];
    
    [screen_img mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(5);
        make.top.mas_equalTo((k_date_view_h - 5) * 0.5);
        make.width.mas_equalTo(sort_img_w);
    }];
    
    [screen_label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(screen_img.mas_left).mas_offset(-lr_space);
        make.width.mas_equalTo(size_screen.width + more_space);
        make.height.mas_equalTo(20);
        make.top.mas_equalTo((k_date_view_h - 20) * 0.5);
        
    }];
    

    return header_view;
    
}

#pragma mark 私有方法 --请求数据
- (void)loadHotelListData{
    
    NSString *url = [MyTool splicingWithUrl:@"admin.php?g=System&c=HotelApp&a=Hotel_search"];
    NSDictionary *dic = @{@"long":@"",@"lat":@"",@"hotel_name":@"",@"begin_time":@"",@"end_time":@"",@"sort":@"",@"type_id":@"",@"pagenow":@"0",@"area_id":@"",@"size":@"10"};
    NSLog(@"酒店首页搜索接口===%@ , dic===%@",url,dic);
    
    [SVProgressHUD showWithStatus:@"加载中..."];
    [[NetworkRequestTool sharedNetworkTool] GET:url parameters:dic finished:^(id result, NSString *error) {
        NSLog(@"酒店首页搜索:%@",result);
        [SVProgressHUD dismiss];
        
       
    }];
}


#pragma mark 私有方法 -- 初始化
-(UIButton *)back_button{
    
    if (_back_button == nil) {
        _back_button = [[UIButton alloc] init];
        [_back_button addTarget:self action:@selector(clickBackButton:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _back_button;
    
}

-(UIButton *)address_button{
    
    if (_address_button == nil) {
        _address_button = [[UIButton alloc] init];
        [_address_button addTarget:self action:@selector(clickAddressButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _address_button;
    
}

-(UILabel *)address_label{
    
    if (_address_label == nil) {
        _address_label = [[UILabel alloc] init];
        _address_label.text = self.address_text;
        _address_label.textColor = RGB(51, 51, 51);
        _address_label.font = [UIFont systemFontOfSize:15.0f];
    }
    return _address_label;
}

-(UISearchBar *)search_bar{
    
    if (_search_bar == nil) {
        
        _search_bar = [[UISearchBar alloc] init];
        _search_bar.backgroundImage = [[UIImage alloc] init];
        _search_bar.showsCancelButton = NO;
        _search_bar.backgroundColor =  RGB(244, 244, 244);
        _search_bar.translucent  = YES;
        _search_bar.delegate     = self;
        _search_bar.placeholder  = self.search_bar_placeholder;
        _search_bar.keyboardType = UIKeyboardTypeDefault;
        [_search_bar setBarTintColor:RGB(244, 244, 244)];
        
        UIImage *searchIcon = [UIImage imageNamed:@"nav_icon_search"];
        [_search_bar setImage:searchIcon forSearchBarIcon:UISearchBarIconSearch state:UIControlStateNormal];
        
        for (UIView* subview in [[_search_bar.subviews lastObject] subviews]) {
            
            if ([subview isKindOfClass:[UITextField class]]) {
                UITextField *textField = (UITextField*)subview;
                //修改输入字体的颜色
                textField.textColor = [UIColor blackColor];
                textField.font = [UIFont systemFontOfSize:14.0f];
                //修改输入框的颜色
                [textField setBackgroundColor:RGB(244, 244, 244)];
                
            } else if ([subview isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
            {
                [subview removeFromSuperview];
            }
        }
        
    }
    return _search_bar;
    
}

-(UITableView *)table_view{
    
    if (_table_view == nil) {
        
        _table_view = [[UITableView alloc] init];
        _table_view.delegate = self;
        _table_view.dataSource = self;
        _table_view.tableFooterView = [[UIView alloc] init];
        _table_view.backgroundColor = RGB(244, 244, 244);
        [_table_view setTableHeaderView:[self tabelHeaderViewInTopSection]];
        
    }
    return _table_view;
}

-(UIButton *)date_button{
    
    if (_date_button == nil) {
        _date_button = [[UIButton alloc] init];
        [_date_button setBackgroundColor:RGB(244, 244, 244)];
        [_date_button addTarget:self action:@selector(clickDateButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _date_button;
    
}

-(UILabel *)date_label{
    
    if (_date_label == nil) {
        _date_label = [[UILabel alloc] init];
        _date_label.text = self.date_text;
        _date_label.textColor = RGB(51, 51, 51);
        _date_label.font = [UIFont systemFontOfSize:13.0f];
    }
    return _date_label;
}

-(UILabel *)timer_label{
    
    if (_timer_label == nil) {
        _timer_label = [[UILabel alloc] init];
        _timer_label.text = self.timer_text;
        _timer_label.textColor = RGB(219, 52, 52);
        _timer_label.font = [UIFont systemFontOfSize:13.0f];
        _timer_label.textAlignment = NSTextAlignmentCenter;
    }
    return _timer_label;
}

-(UIButton *)sort_button{
    
    if (_sort_button == nil) {
        _sort_button = [[UIButton alloc] init];
        [_sort_button addTarget:self action:@selector(clickSortButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sort_button;
    
}

-(UIButton *)screen_button{
    
    if (_screen_button == nil) {
        _screen_button = [[UIButton alloc] init];
        [_screen_button addTarget:self action:@selector(clickScreenButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _screen_button;
    
}

#pragma mark 私有方法 -- 点击事件
-(void)clickBackButton:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)clickAddressButton:(UIButton *)sender{
    
}

-(void)clickDateButton:(UIButton *)sender{
    
}

-(void)clickSortButton:(UIButton *)sender{
    
}

-(void)clickScreenButton:(UIButton *)sender{
    
}

#pragma mark UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return 5;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifity1 = @"cell";
    SCHotelHomeCell *cell = [tableView dequeueReusableCellWithIdentifier:identifity1];
    if (cell == nil) {
        cell = [[SCHotelHomeCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifity1 ];
    }
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return k_hotel_cell_h;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
    [self.table_view deselectRowAtIndexPath:indexPath animated:NO];
    SCHotelInfoController *vc = [[SCHotelInfoController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    
}




@end
